<!DOCTYPE html>
<html lang="en">
<head>
	 <meta charset="UTF-8">
	 <title>Ajax上传文件</title>
	<script src="http://www.jq22.com/jquery/jquery-1.9.1.js"></script>
</head>
<body>
用户名：<input id="username" type="text"><br>
用户图像：<input id="carid_image_z" type="file" onchange="preview(this)"><br>
<div id="preview"></div>
<input type="button" id="btnClick" value="上传">
<script>
	$("#btnClick").click(function () {
		var formData = new FormData();
		formData.append("id", 'd0aaf17a-6782-4edb-9ea5-b274d611e1aa');
		formData.append("ts", '20180110');
		formData.append("device_id", '866666030863998');
		formData.append("package_id", '');
		formData.append("sign", 'hash_hmac(\'sha512\', _echo($post_data), \'7bd5de940d924ce7a038d5ae0046dd09383fb04d4b6848cc834adbac0ce15f5a\');');
		console.log(formData);


		$.ajax({
			url: 'http://180.169.48.172:38790/mapi_v1/get_package',
			type: 'post',
			data: formData,
			processData: false,
			contentType: false,
			headers: {
				'Content-type': 'application/x-www-form-urlencoded',
				'sign': 'uid:d0aaf17a-6782-4edb-9ea5-b274d611e1aa'
			},
			success: function (msg) {
				console.log(msg);
			},error: function(){
				console.log("失败了");
			}
		});
	});
	function preview(file) {
		var prevDiv = document.getElementById('preview');
		if (file.files && file.files[0]) {
			var reader = new FileReader();
			reader.onload = function (evt) {
				prevDiv.innerHTML = '<img src="' + evt.target.result + '" />';
			}
			reader.readAsDataURL(file.files[0]);
		} else {
			prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
		}
	}
</script>
</body>
</html>
